var NNModel           = require('./NNModel');
var NNProto           = require('./NNProto');
var RoomProto         = require('../../API/RoomProto');
var HallApi           = require('../../API/HallAPI');
var RoomAPI           = require('../../API/RoomAPI');
var RoomMessageRouter = 'game.gameHandler.roomMessageNotify';
var GameMessageRouter = 'game.gameHandler.gameMessageNotify';

cc.Class({
	extends: cc.Component,
	properties: {
	},

	start: function () {
		this.cardsNode = cc.find('cards', this.node);
		this.cardTypeNode = cc.find('typebg', this.node);
		this.cardsNodeArray = [];
		this.cardsNodePosArray = [];
		for (let i = 1; i <= 5; ++i) {
			let node = this.cardsNode.getChildByName('card'+i);
			this.cardsNodeArray.push(node);
			this.cardsNodePosArray.push(node.getPosition());
			cc.find('cards/mask'+i, this.node).active = false;
		}
		this.cardTypeNodePos = this.cardTypeNode.getPosition();
		this.cardsNode.active = false;
		this.cardTypeNode.active = false; 

		Global.MessageCallback.addListener('RoomMessagePush', this);
		Global.MessageCallback.addListener('GameMessagePush', this);
		Global.MessageCallback.addListener('ReConnectSuccess', this);
		Global.MessageCallback.addListener('UpdateGameCardBg', this);
	},

	setIndex: function (index) {
		this.index = index;
	},

	getChairID: function () {
		if (NNModel.getMyChairID() >= NNModel.getChairCount()) {
			return this.index;
		}
		else {
			return (NNModel.getMyChairID()+this.index)%NNModel.getChairCount();
		}
	},

	gameInit () {
		if (typeof(this.index) != 'number') { return; }
		let cards = NNModel.getHandCardsByChairID(this.getChairID());
		this.cardsNode.active = false;
		this.cardTypeNode.active = false;
		if (cards) {
			for (let i = 0; i < this.cardsNodeArray.length; ++i) {
				this.cardsNodeArray[i].setPosition(this.cardsNodePosArray[i]);
				this.cardsNodeArray[i].setScale(1);
			}
			this.node.active = true;
			this.sendCards(); 
			let canShowType = true;
			for (let i = 0; i < cards.length; ++i) {
				if (cards[i] == 0) {
					canShowType = false;
				}
			}
			if (canShowType && NNModel.getShowCards(this.getChairID())) {
				this.showType();
			}
			this.showMask();
		}
	},

	messageCallbackHandler: function(router, msg) {
		if(!NNModel.getGameInited()) { return; }
		if (router === 'RoomMessagePush') {
			if(msg.type === RoomProto.GET_ROOM_SCENE_INFO_PUSH) {
				this.gameInit();
			}
			else if(msg.type === RoomProto.USER_LEAVE_ROOM_RESPONSE) {
				/* if(msg.data.chairID == this.getChairID()) {
					this.node.active = false;
				} */
			}
			else if(msg.type === RoomProto.OTHER_USER_ENTRY_ROOM_PUSH) {
				if (msg.data.roomUserInfo.chairID == this.getChairID()) {
					this.gameInit();
				}
			}
			else if(msg.type === RoomProto.USER_LEAVE_ROOM_PUSH) {
				if (msg.data.roomUserInfo.chairID == this.getChairID()) {
					this.node.active = false;
				}
			}
			else if(msg.type === RoomProto.USER_CHANGE_SEAT_PUSH) {
				this.gameInit();
			}
			else if(msg.type === RoomProto.USER_READY_PUSH) {
				if (msg.data.chairID == NNModel.getMyChairID()) {
					this.cardsNode.active = false;
					this.cardTypeNode.active = false;
					for (let i = 1; i <= 5; ++i) {
						cc.find('cards/mask'+i, this.node).active = false;
					}
				}
			}
		}
		else if(router === 'GameMessagePush') {
			if(msg.type === NNProto.GAME_SHOW_CARDS_PUSH) {
				if (this.getChairID() == msg.data.chairID) {
					if (!msg.data.cuopai) {
						this.sendCards();
						this.showType(true);
					}
					else {
						this.showFourCards();
					}
				}
			}
			else if(msg.type === NNProto.GAME_SEND_CARDS_PUSH) {
				this.sendCards(true);
			}
			else if(msg.type === NNProto.GAME_STATUS_PUSH) {
				if (msg.data.gameStatus == NNProto.gameStatus.SEND_CARDS) {
					this.cardsNode.active = false;
					this.cardTypeNode.active = false;
					for (let i = 1; i <= 5; ++i) {
						cc.find('cards/mask'+i, this.node).active = false;
					}
				} 
			}
		}
		else if(router === 'UpdateGameCardBg') {
			this.sendCards(); 
		}
	},

	getCard0Url: function () {
		let cardbg = cc.sys.localStorage.getItem('cardBg');
		let url = 'Game/Common/card_back_0';
		if (cardbg == 'cardBg1') {
			url = 'Game/Common/card_back_0';
		}
		else if (cardbg == 'cardBg2') {
			url = 'Game/Common/card_back_1';
		}
		else if (cardbg == 'cardBg3') {
			url = 'Game/Common/card_back_2';
		}
		else if (cardbg == 'cardBg4') {
			url = 'Game/Common/card_back_3';
		}
		return url;
	},

	showFourCards: function () {
		let cards = NNModel.getHandCardsByChairID(this.getChairID());
		if (!cards) { return; }
		for (let i = 0; i < this.cardsNodeArray.length-1; ++i) {
			Global.CCHelper.updateSpriteFrame('Game/Cards/'+cards[i], this.cardsNodeArray[i].getComponent(cc.Sprite));
		}
	},

	sendCards: function (animal) {
		let cards = NNModel.getHandCardsByChairID(this.getChairID());
		if (!cards) { return; }
		for (let i = 0; i < this.cardsNodeArray.length; ++i) {
			let url = 'Game/Cards/'+cards[i];
			if (cards[i] == 0) { url = this.getCard0Url(); }
			Global.CCHelper.updateSpriteFrame(url, this.cardsNodeArray[i].getComponent(cc.Sprite));
			this.cardsNodeArray[i].stopAllActions();
			this.cardsNodeArray[i].setPosition(this.cardsNodePosArray[i]);
		}
		this.cardsNode.active = true;
		this.node.active = true;
		if (animal) {
			let node_pos = this.node.getPosition();
			let audio_func = cc.callFunc(() => {
				Global.AudioManager.playSound('Game/Common/Audio/foldpai');
			});
			for (let i = 0; i < this.cardsNodeArray.length; ++i) {
				let card_node = this.cardsNodeArray[i];
				let card_node_pos = card_node.getPosition();
				card_node.setPosition(cc.v2(
					card_node_pos.x-node_pos.x,
					card_node_pos.y-node_pos.y
				));
				card_node.setScale(0);
				card_node.runAction(cc.sequence(
					cc.delayTime(0.1*i),
					cc.moveTo(0.2, this.cardsNodePosArray[i]),
					audio_func
				));
				card_node.runAction(cc.sequence(
					cc.delayTime(0.1*i),
					cc.scaleTo(0.2, 1)
				));
			}
		}
	},

	showType: function (animal) {
		let cards = NNModel.getHandCardsByChairID(this.getChairID());
		if (!cards) { return; }
		if (this.getChairID() != NNModel.getMyChairID()) {
			this.sendCards();
		}
		this.showMask();
		this.cardTypeNode.active = true;
		let type = NNModel.getNNLogic().getCardsType(cards);
		let url = 'Game/Niuniu/';
		let audio = 'Game/Niuniu/NNAudio/woman/';
		if (type == NNProto.cardsType.MEINIU) {
			url += 'wuniu';
			audio += 'f0_nn0';
		}
		else if (type == NNProto.cardsType.YOUNIU) {
			let num = NNModel.getNNLogic().getNiuNumber(cards);
			url += ['wuniu', 'niu1', 'niu2', 'niu3', 'niu4', 'niu5', 'niu6', 'niu7', 'niu8', 'niu9', 'niuniu'][num];
			audio += ['f0_nn0', 'f0_nn1', 'f0_nn2', 'f0_nn3', 'f0_nn4', 'f0_nn5', 'f0_nn6', 'f0_nn7', 'f0_nn8', 'f0_nn9', 'f0_nn10'][num];
		}
		else if (type == NNProto.cardsType.NIUNIU) {
			url += 'niuniu';
			audio += 'f0_nn10';
		}
		else if (type == NNProto.cardsType.SHUNZINIU) {
			url += 'shunziniu';
			audio += 'f0_nn12';
		}
		else if (type == NNProto.cardsType.YINNIU) {
			url += 'yinniu';
		}
		else if (type == NNProto.cardsType.TONGHUANIU) {
			url += 'tonghuaniu';
			audio += 'f0_nn14';
		}
		else if (type == NNProto.cardsType.WUHUANIU) {
			url += 'wuhuaniu';
			audio += 'f0_nn13';
		}
		else if (type == NNProto.cardsType.HULUNIU) {
			url += 'huluniu';
			audio += 'f0_nn15';
		}
		else if (type == NNProto.cardsType.WUXIAONIU) {
			url += 'wuxiaoniu';
			audio += 'f0_nn17';
		}
		else if (type == NNProto.cardsType.ZHADANNIU) {
			url += 'zhadanniu';
			audio += 'f0_nn16';
		}
		else if (type == NNProto.cardsType.YITIAOLONG) {
			url += 'yitiaolong';
		}
		else if (type == NNProto.cardsType.TONGHUASHUN) {
			url += 'tonghuashun';
		}
		let typeNode = this.cardTypeNode.getChildByName('type');
		Global.CCHelper.updateSpriteFrame(url, typeNode.getComponent(cc.Sprite));
		this.cardTypeNode.stopAllActions();
		this.cardTypeNode.setPosition(this.cardTypeNodePos);
		if (animal) {
			let offx = (this.index == 0)? 125:50;
			this.cardTypeNode.x = this.cardTypeNode.x+offx;
			this.cardTypeNode.stopAllActions();
			this.cardTypeNode.runAction(cc.moveTo(0.2, this.cardTypeNodePos).easing(cc.easeIn(3.0)));
			let user = NNModel.getUserByChairID(this.getChairID());
			if (user && user.userInfo.sex == 0) {
				audio = audio.replace(/woman/, "man");
				audio = audio.replace(/f0_nn/, "f1_nn");
			}
			Global.AudioManager.playSound(audio);
		}
	},

	showMask: function () {
		let cards = NNModel.getHandCardsByChairID(this.getChairID());
		let hasMask = true;
		if (!cards) { hasMask = false; }
		for (let i = 0; i < cards.length; ++i) {
			if (cards[i] == 0) { hasMask = false; }
		}
		let type = NNModel.getNNLogic().getCardsType(cards);
		if (type != NNProto.cardsType.YOUNIU && type != NNProto.cardsType.NIUNIU) {
			hasMask = false;
		}
		if (hasMask) {
			for (let i = 0; i < cards.length; ++i) {
				for (let j = i+1; j < cards.length; ++j) {
					for (let k = j+1; k < cards.length; ++k) {
						if ((NNModel.getNNLogic().getCardValue(cards[i])+NNModel.getNNLogic().getCardValue(cards[j])+NNModel.getNNLogic().getCardValue(cards[k]))%10 == 0) {
							cc.find('cards/mask'+(i+1), this.node).active = true;
							cc.find('cards/mask'+(j+1), this.node).active = true;
							cc.find('cards/mask'+(k+1), this.node).active = true;
							return;
						}
					}
				}
			}
		}
		else {
			for (let i = 1; i <= 5; ++i) {
				cc.find('cards/mask'+i, this.node).active = false;
			}
		}
	},

	onDestroy: function () {
		Global.MessageCallback.removeListener('RoomMessagePush', this);
		Global.MessageCallback.removeListener('GameMessagePush', this);
		Global.MessageCallback.removeListener('ReConnectSuccess', this);
		Global.MessageCallback.removeListener('UpdateGameCardBg', this);
	},
});
